﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;



namespace VASJ.Service.Library.Manager
{
    public class PermissionManager : BaseManager
    {
         /// <summary>
        /// Init
        /// </summary>
        /// <param name="_ApplicationName"></param>
        public PermissionManager(string _ApplicationName)
            : base(_ApplicationName)
        {
        }

        #region private method

        private bool checkAccessToMain(Guid userid, int mainProjectId)
        {
            var result = from uip in this.mdb.AHSUserInProjects
                         where ((uip.Projectid == mainProjectId) && (uip.Userid == userid))
                         select uip;
            return result.Count() > 0;
        }

        private bool checkAccessToMain(IEnumerable<Guid> orgids, int mainProjectId)
        {

            var orgInPrjs = from oip in this.mdb.AHSOrganizationInProjects 
                            where oip.Projectid == mainProjectId
                            select oip;
            var ret = false;
            foreach (Guid guid in orgids)
            {
                foreach (var orgInPrj in orgInPrjs)
                {
                    if (orgInPrj.Orgid == guid)
                    {
                        ret = true;
                        break;
                    }
                }
                if (ret == true)
                    break;
            }
            return ret;
        }

        private bool checkRoleToMain(Guid userid, int mainProjectId, string role)
        {
            var result = from uip in this.mdb.AHSUserInProjects
                         where ((uip.Projectid == mainProjectId) && (uip.Userid == userid) && (uip.AHSAspnetRole.Rolename == role))
                         select uip;
            
            return result.Count() > 0;
        }

        private bool checkRoleToMain(IEnumerable<Guid> orgids, int mainProjectId, string role)
        {
            
            var orgInPrjs = from oip in this.mdb.AHSOrganizationInProjects
                            where ((oip.Projectid == mainProjectId) && (oip.AHSAspnetRole.Rolename == role))
                            select oip;
            var ret = false;
            foreach (Guid guid in orgids)
            {
                foreach (var orgInPrj in orgInPrjs)
                {
                    if (orgInPrj.Orgid == guid)
                    {
                        ret = true;
                        break;
                    }
                }
                if (ret == true)
                    break;
            }
            return ret;
            //var result = orgInProjectTbl.Select(a => a.Projectid == mainProjectId && orgid.Contains(a.Orgid) && role == a.CSDbUserRole.RoleASPName).Any();
        }

        #endregion private method
        public  bool IsMainProjectAccessibleByUser(string user, int mainProjectId)
        {
            if (user == null)
            {
                return false;
            }
            else if (MembershipHelper.CheckAdmin(user))
            {
                return true;
            }
            else
            {
                var userid = MembershipHelper.GetUserId(user);
                
                var orgs = (from uio in this.mdb.AHSUsersInOrganizations
                             where (uio.UserId == userid)
                             select uio.OrgId).AsEnumerable();


                return userid.HasValue ? (checkAccessToMain(userid.Value, mainProjectId) || checkAccessToMain(orgs, mainProjectId)) : false;
            }
        }

        public bool IsMainProjectAccessibleByOrganization(Guid orgid, int mainProjectId)
        {
            if (orgid == null)
            {
                return false;
            }
            else
            {
                return checkAccessToMain(orgid, mainProjectId);
            }
        }


    }
}